package acme.bloodtime.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import modelo.IArmaDAO;

import dao.api.DataObject;

/**
 * 
 * @author Manuel A. Quintero V.
 * 
 */

public class ArmaDAO extends ArticuloAbstractDAO implements IArmaDAO {

	public ArmaDAO() {
		// Empty
	}

	@Override
	public void createTable() throws SQLException {
		StringBuffer strbuf;

		ArticuloDAO articuloDAO = new ArticuloDAO();

		// ----------------------------------------

		dropTable();

		// ----------------------------------------
		strbuf = new StringBuffer();

		strbuf.append("CREATE TABLE ");
		strbuf.append(getTableName());
		strbuf.append(" (");
		strbuf.append(articuloDAO.getTableName() + ArmaDO.ID);
		strbuf.append("  INT REFERENCES ");
		strbuf.append(articuloDAO.getTableName());
		strbuf.append(" (");
		strbuf.append(ArmaDO.ID);
		strbuf.append("),");
		strbuf.append(ArmaDO.HAB_ARMAS_REQUERIDA);
		strbuf.append(" INT,    ");
		strbuf.append(ArmaDO.CANT_MEJORA_FUERZA);
		strbuf.append(" INT,    ");
		strbuf.append(" PRIMARY KEY   ");
		strbuf.append(" (");
		strbuf.append(articuloDAO.getTableName() + ArmaDO.ID);
		strbuf.append("))");

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

	}

	@Override
	public void insert(DataObject dataObject) throws SQLException {
		checkCache(dataObject, CHECK_INSERT);
		checkClass(dataObject, ArmaDO.class, CHECK_INSERT);

		ArmaDO armaDO = (ArmaDO) dataObject;

		armaDO.setId(internalInsertArticulo(dataObject, ArmaDO.class));
		StringBuffer strbuf = new StringBuffer();

		strbuf.append("INSERT INTO ");
		strbuf.append(getTableName());
		strbuf.append(" VALUES (");
		strbuf.append(armaDO.getId());
		strbuf.append(", ");
		strbuf.append(armaDO.getHabArmasRequerida());
		strbuf.append(", ");
		strbuf.append(armaDO.getCantMejoraFuerza());
		strbuf.append(")");

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		dtaSession.add(dataObject);

	}

	@Override
	public void update(DataObject dataObject) throws SQLException {
		checkCache(dataObject, CHECK_UPDATE);
		checkClass(dataObject, ArmaDO.class, CHECK_UPDATE);

		ArticuloDAO articuloDAO = new ArticuloDAO();

		ArmaDO armaDO = (ArmaDO) dataObject;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append("UPDATE ");
		strbuf.append(getTableName());
		strbuf.append(" SET ");

		strbuf.append(ArmaDO.HAB_ARMAS_REQUERIDA);
		strbuf.append(" = ");
		strbuf.append(armaDO.getHabArmasRequerida());

		strbuf.append(", ");

		strbuf.append(ArmaDO.CANT_MEJORA_FUERZA);
		strbuf.append(" = ");
		strbuf.append(armaDO.getCantMejoraFuerza());

		strbuf.append(" WHERE ");
		strbuf.append(articuloDAO.getTableName() + ArmaDO.ID);
		strbuf.append(" = ");
		strbuf.append(armaDO.getId());

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		internalUpdateArticulo(dataObject, ArmaDO.class);

	}

	@Override
	public void delete(DataObject dataObject) throws SQLException {
		checkCache(dataObject, CHECK_DELETE);
		checkClass(dataObject, ArmaDO.class, CHECK_DELETE);

		ArticuloDAO articuloDAO = new ArticuloDAO();

		StringBuffer strbuf = new StringBuffer();

		strbuf.append("DELETE FROM ");
		strbuf.append(getTableName());

		strbuf.append(" WHERE ");
		strbuf.append(articuloDAO.getTableName() + DataObject.ID);
		strbuf.append(" = ");
		strbuf.append(dataObject.getId());

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		internalDelete(dataObject, ArmaDO.class);

		dtaSession.del(dataObject);

	}

	@Override
	protected DataObject resultSetToDO(ResultSet rs) throws SQLException {
		ArmaDO ret = //
		(ArmaDO) dtaSession.getDtaByKey( //
				ArmaDO.class, rs.getInt(ArmaDO.ID));
		if (ret != null) {
			return ret;
		}
		
		ret = new ArmaDO();

		ret.setId/*					*/(rs.getInt(ArmaDO.ID));
		ret.setNombre/*				*/(rs.getString(ArmaDO.NOMBRE));
		ret.setDescripcion/*		*/(rs.getString(ArmaDO.DESCRIPCION));
		ret.setImagen/*				*/(rs.getBytes(ArmaDO.IMAGEN));
		ret.setPrecioCompra/*		*/(rs.getFloat(ArmaDO.PRECIO_COMPRA));
		ret.setPrecioVenta/*		*/(rs.getFloat(ArmaDO.PRECIO_VENTA));
		ret.setHabArmasRequerida/*	*/(rs.getInt(ArmaDO.HAB_ARMAS_REQUERIDA));
		ret.setCantMejoraFuerza/*	*/(rs.getInt(ArmaDO.CANT_MEJORA_FUERZA));

		return (ArmaDO) dtaSession.add(ret);
	}

}
